package com.yudaxian.bi.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.yudaxian.bi.bizmq.BiMessageProducer;
import com.yudaxian.bi.common.ErrorCode;
import com.yudaxian.bi.exception.BusinessException;
import com.yudaxian.bi.mapper.ChartMapper;
import com.yudaxian.bi.model.dto.chart.ChartAddRequest;
import com.yudaxian.bi.model.entity.Chart;
import com.yudaxian.bi.model.enums.PageSize;
import com.yudaxian.bi.model.query.ChartQuery;
import com.yudaxian.bi.model.query.SimplePage;
import com.yudaxian.bi.model.vo.PaginationResultVO;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.yudaxian.bi.service.ChartService;
import com.yudaxian.bi.utils.StringTools;


/**
 * 图表信息表 业务接口实现
 */
@Service
public class ChartServiceImpl implements ChartService {

	@Resource
	private ChartMapper<Chart, ChartQuery> chartMapper;

	@Resource
	private BiMessageProducer biMessageProducer;

	/**
	 * 根据条件查询列表
	 */
	@Override
	public List<Chart> findListByParam(ChartQuery param) {
		return this.chartMapper.selectList(param);
	}

	/**
	 * 根据条件查询列表
	 */
	@Override
	public Integer findCountByParam(ChartQuery param) {
		return this.chartMapper.selectCount(param);
	}

	/**
	 * 分页查询方法
	 */
	@Override
	public PaginationResultVO<Chart> findListByPage(ChartQuery param) {
		int count = this.findCountByParam(param);
		int pageSize = param.getPageSize() == null ? PageSize.SIZE15.getSize() : param.getPageSize();

		SimplePage page = new SimplePage(param.getPageNo(), count, pageSize);
		param.setSimplePage(page);
		param.setOrderBy(" createTime desc ");
		List<Chart> list = this.findListByParam(param);
		PaginationResultVO<Chart> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
		return result;
	}

	/**
	 * 新增
	 */
	@Override
	public Integer add(ChartAddRequest bean) {
		return this.chartMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	@Override
	public Integer addBatch(List<Chart> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.chartMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	@Override
	public Integer addOrUpdateBatch(List<Chart> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.chartMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 多条件更新
	 */
	@Override
	public Integer updateByParam(Chart bean, ChartQuery param) {
		StringTools.checkParam(param);
		return this.chartMapper.updateByParam(bean, param);
	}

	/**
	 * 多条件删除
	 */
	@Override
	public Integer deleteByParam(ChartQuery param) {
		StringTools.checkParam(param);
		return this.chartMapper.deleteByParam(param);
	}

	/**
	 * 根据Id获取对象
	 */
	@Override
	public Chart getChartById(Long id) {
		if (id == null) {
			throw new BusinessException(ErrorCode.PARAMS_ERROR);
		}
		return this.chartMapper.selectById(id);
	}

	/**
	 * 根据Id修改
	 */
	@Override
	public Integer updateChartById(Chart bean, Long id) {
		return this.chartMapper.updateById(bean, id);
	}

	/**
	 * 根据Id删除
	 */
	@Override
	public Integer deleteChartById(Long id) {
		return this.chartMapper.deleteById(id);
	}

	@Override
	public Integer saveChart(Chart bean) {
		return this.chartMapper.saveChart(bean);
	}

	// 每天上午十点，下午两点，四点
	@Scheduled(cron = "0 0 10,14,16 * * ?")
	@Async
	@Override
	public void refreshChart() {

		List<Chart> chartList = chartMapper.selectFailedChart();

		chartList.forEach(chart -> {
			biMessageProducer.sendMessage(String.valueOf(chart.getId()));
		});

	}
}